using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using EntLibEC.Cmn;

using EntLibEC.Info.Basic;
using EntLibEC.Info.Sale;

using EntLibEC.Biz;
using EntLibEC.Biz.Sale;

namespace EntLibEC.View.Sale
{
	/// <summary>
	/// Summary description for SaleGiftRelateMoneyOpt.
	/// </summary>
	public partial class SaleGiftRelateMoneyOpt : PageBase
	{

		protected string bodyOnLoad = "";
		private int sysno;
		private string opt;
		protected EntLibEC.View.Cmn.BiStatus ucStatus;
		protected EntLibEC.View.Cmn.StockProduct ucProduct;
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			if ( Request.QueryString["sysno"] != null)
			{
				try
				{
					this.sysno = Convert.ToInt32(Request.QueryString["sysno"].ToString());
				}
				catch
				{
					Assert(lblMessage, "sysno is not an integer", -1);
					return;
				}
			}

			if ( Request.QueryString["opt"] == null )
			{
				Assert(lblMessage, "opt missing", -1);
				return;
			}
			else
			{
				this.opt = Request.QueryString["opt"].ToString();
			}

			InitForm();
			if ( !Page.IsPostBack)
			{
				ucStatus.BindStatus(false);
				BindForm();
			}
		}

		public void InitForm()
		{
			btnUpdate.Visible = false;
			btnAdd.Visible = false;
			ucStatus.Enabled = false;
			ucProduct.Enabled = false;

			if ( opt == "update")
			{
				btnUpdate.Visible = true;
				ucStatus.Enabled = true;
			}
			else
			{
				btnAdd.Visible = true;
				ucProduct.Enabled = true;
			}
		}

		private void BindForm()
		{
			if ( opt == "update" )
			{
				SaleGiftRelateMoneyInfo oInfo = SaleGiftRelateMoneyManager.GetInstance().Load(this.sysno);

				if ( oInfo == null )
				{
					Assert(lblMessage, "get item information failed", -1);
				}
				else
				{
					txtGiftDesc.Text = oInfo.GiftDesc;
					ucProduct.ProductSysNo = oInfo.ProductSysNo;
					txtMoneyStart.Text = oInfo.MoneyStart.ToString(AppConst.DecimalFormat);
					txtMoneyEnd.Text = oInfo.MoneyEnd.ToString(AppConst.DecimalFormat);
					ucStatus.StatusSysNo = oInfo.Status;
				}
			}
			else
			{
				ucStatus.StatusSysNo = (int)AppEnum.BiStatus.InValid;
			}
		}

		private bool CheckForm()
		{
			if ( ucProduct.ProductSysNo == AppConst.IntNull )
			{
				Assert(lblMessage, "please select a product", -1);
				return false;
			}
			if ( txtMoneyStart.Text.Trim() == "")
			{
				Assert(lblMessage, "money start can't be empty", -1);
				return false;
			}
			else
			{
				try
				{
					Convert.ToDecimal(txtMoneyStart.Text.Trim());
				}
				catch
				{
					Assert(lblMessage, "money start should be a decimal", -1);
					return false;
				}
			}

			if ( txtMoneyEnd.Text.Trim() == "")
			{
				Assert(lblMessage, "money end can't be empty", -1);
				return false;
			}
			else
			{
				try
				{
					Convert.ToDecimal(txtMoneyEnd.Text.Trim());
				}
				catch
				{
					Assert(lblMessage, "money end should be a decimal", -1);
					return false;
				}
			}

			if ( txtGiftDesc.Text.Trim() == "")
			{
				Assert(lblMessage, "Gift Desc can't be empty", -1);
				return false;
			}
			
			return true;
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

		protected void btnAdd_Click(object sender, System.EventArgs e)
		{
			if ( !base.CheckAccess((int)AppEnum.Privilege.SaleGiftRelateMoney, false, lblMessage) )
				return;

			if (!CheckForm())
				return;

			try
			{
				SaleGiftRelateMoneyInfo oInfo = new SaleGiftRelateMoneyInfo();
				oInfo.CreateTime = DateTime.Now;
				oInfo.CreateUserSysNo = base.sInfo.User.SysNo;
				oInfo.GiftDesc = txtGiftDesc.Text.Trim();
				oInfo.ProductSysNo = ucProduct.ProductSysNo;
				oInfo.MoneyStart = Convert.ToDecimal(txtMoneyStart.Text.Trim());
				oInfo.MoneyEnd = Convert.ToDecimal(txtMoneyEnd.Text.Trim());
				oInfo.Status = (int)AppEnum.BiStatus.InValid;

				SaleGiftRelateMoneyManager.GetInstance().Insert(oInfo);
				bodyOnLoad = "javascript:window.opener.__doPostBack('btnSearch','btnSearch');window.close(); return false;";
			}
			catch(BizException exp)
			{
				Assert(lblMessage, exp.Message, -1);
			}
			catch(Exception exp)
			{
				Assert(lblMessage, "insert error, see log", -1);
				ErrorLog.GetInstance().Write(exp.ToString());
			}
		}

		protected void btnUpdate_Click(object sender, System.EventArgs e)
		{
			if ( !base.CheckAccess((int)AppEnum.Privilege.SaleGiftRelateMoney, false, lblMessage) )
				return;

			if (!CheckForm())
				return;

			try
			{
				//Hashtalbe---->
				Hashtable ht = new Hashtable(5);
				ht.Add("SysNo", this.sysno);
				ht.Add("GiftDesc", txtGiftDesc.Text.Trim());
				ht.Add("MoneyStart", Convert.ToDecimal(txtMoneyStart.Text.Trim()));
				ht.Add("MoneyEnd", Convert.ToDecimal(txtMoneyEnd.Text.Trim()));
				ht.Add("Status", ucStatus.StatusSysNo);

				SaleGiftRelateMoneyManager.GetInstance().Update(ht);
				bodyOnLoad = "javascript:window.opener.__doPostBack('btnSearch','btnSearch');window.close(); return false;";
			}
			catch(BizException exp)
			{
				Assert(lblMessage, exp.Message, -1);
			}
			catch(Exception exp)
			{
				Assert(lblMessage, "update error, see log", -1);
				ErrorLog.GetInstance().Write(exp.ToString());
			}
		
		}
	}
}
